<?php


function curl_http($url,$data=null,$method='GET'){
	if(is_array($data)){
		$data = http_build_query($data);
	}
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, false);
	if($method!='GET'){
		curl_setopt($ch, CURLOPT_POST, 1);
	}
	if($data!=null){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);	
	}
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  //结果是否显示出来，1不显示，0显示    
	//判断是否https
	if(strpos($url,'https://')!==false){
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
		curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
	}
	
	
	$data = curl_exec($ch);
	curl_close($ch);
	if($data === FALSE) 
	{ 
	  $data = "curl Error:".curl_error($ch);
	} 
	return $data;
}

function Redirect($url,$info=null,$sec=3){
	if(is_null($info)){
		header("Location:$url");
		}else{
			//header("Refersh:$sec;URL=$url");
			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="refresh" content="'.$sec.';URL='.$url.'">';
			echo $info;
		}
		die; 
	
}
$config = include_once("config.php");
$data = file_get_contents('url.txt');
if($data){
	$urls = explode('|',$data);
	$res = curl_http($urls[0]);
	$dom = new DOMDocument();
	@$dom->loadHTML($res);
	$dom->normalize();
	$xpath = new DOMXPath($dom);
	if($config['title']){
		$titles = $xpath->query($config['title']);
		$title = $titles->item(0)->nodeValue;
	}else{
		$title = $config['chapter'];
	}
	
/*
object(DOMElement)#3 (18) { ["tagName"]=> string(2) "h1" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "h1" ["nodeValue"]=> string(7) "第1章" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "h1" ["baseURI"]=> NULL ["textContent"]=> string(7) "第1章" }


*/
	echo $title;
	$bodys = $xpath->query($config['detailpath']);
	$body = $bodys->item(0)->nodeValue;
	if($config['replace']){
		$replace = explode('$$$',$config['replace']);
		$a1 = [];
		$a2 = [];
		foreach($replace as $v){
			$vs = explode('|',$v);
			$a1[] = $vs[0];
			$a2[] = $vs[1];
		}
		$body = str_replace($a1,$a2,$body);
	}
	if(!is_dir($config['book'])){
		mkdir($config['book'],777);
	}
	file_put_contents($config['book'].'/'.$title.'.txt',$body);
	array_shift($urls);//删除第一个链接
	if(count($urls)){
		file_put_contents('url.txt',implode('|',$urls));
		$config['chapter'] = $config['chapter']+1;
		file_put_contents('config.php','<?php return '.var_export($config, true).';?>');
		Redirect('core.php','已下载，正在下载下一个章节...');
	}else{
		echo '下载完毕！小说目录['.$config['book'].']';
		$config['chapter'] = 1;
		$config['book'] +=1 ;
		file_put_contents('config.php','<?php return '.var_export($config, true).';?>');
		
		Redirect('index.php');
	}
	
	exit;
	
}


$url = $_GET['url'];
$x1 = explode('/',$url);
$www = $x1[0].'//'.$x1[2];
$html = curl_http($url);
$books = $config['book'];
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->normalize();
$xpath = new DOMXPath($dom);
$hrefs = $xpath->query($config['listpath']);

$lists = [];
echo '正在采集书籍目录<br>';
for ($i = 0; $i < $hrefs->length; $i++) {
 
$href = $hrefs->item($i);
 
$linktext = $href->nodeValue;
 
echo $linktext.'<br>';
$lists[] = $www.$linktext;
}
if(!count($lists)){
	echo '没有采集到任何内容！';
	Redirect('index.php');
	exit;
}
file_put_contents('url.txt',implode('|',$lists));
echo '目录采集完毕！';
Redirect('core.php','准备采集小说内容....');
